package com.itheima.controller;

import com.itheima.constant.Result;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/8 19:00
 */

@RestController
@RequestMapping("/valideCode")
public class ValideCodeController {

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping("/send4Order")
    public Result send4Order(@RequestBody Map map) {
        try {
            //1.获取手机号
            String telephone = map.get("telephone").toString();
            //2.生成一个随机的验证码
            String code = RandomStringUtils.randomNumeric(4);
            //将验证码村存放到redis中;存活时间为10分钟;
            jedisPool.getResource().setex(telephone + "_001", 6000, code);
            return new Result(true, "验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "验证码发送失败,请检查");
        }
    }

    //用于发送登陆时的验证码;
    @RequestMapping("/send4Login")
    public Result send4Login(@RequestBody Map map) {
        try {
            //1.获取手机号
            String telephone = map.get("telephone").toString();
            //2.生成一个随机的验证码
            String code = RandomStringUtils.randomNumeric(6);
            //将验证码村存放到redis中;存活时间为10分钟;
            jedisPool.getResource().setex(telephone + "_002", 1000, code);
            return new Result(true, "验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "验证码发送失败,请检查");
        }
    }
}
